var main = {
    pid : 0 ,
    user : null ,
    shop : null ,
    photos : [] ,
    photoIndex : 0 ,
    search : urlParse() ,
    ls : window.localStorage ,
    init : function () {
        var self = this;
        self.user = JSON.parse(self.ls.getItem('user'));
        self.shop = JSON.parse(self.ls.getItem('shop'));
        document.title = self.shop.name + '欢迎您,' + self.user.nickName;
        self.getPhotos();
        self.events();
    } ,
    events : function () {
        var self = this;
        // 查看大图
        $('.photos').tap(function (e) {
            self.initBigPhoto(e.target);
        });
        // 关闭大图
        $('.bigPhoto').touchstart(function (e) {
            e.stopPropagation();
        });
        // 关闭大图
        $('.bigPhoto img').tap(function (e) {
            $('.bigPhoto').addClass('none');
            setTimeout(function () {
                $('.backstep').removeClass('none');
            } , 200);
            $('.comment-box').addClass('none');
        });
        // 点赞
        $('.like').touchstart(function () {
            self.insertLike();
        });
        // 打开评论框
        $('.comment').touchstart(function () {
            $('.comment-box').removeClass('none');
        });
        // 提交评论
        $('.commitComment').touchstart(function () {
            self.comment();
        });
        // 关闭大图模式
        $('.closepage').touchstart(function () {
            $('.bigPhoto').addClass('none');
            setTimeout(function () {
                $('.backstep').removeClass('none');
            } , 200);
            $('.comment-box').addClass('none');
        });
    } ,
    initBigPhoto : function (target) {
        var $photo = target.parentNode;
        var src = target.attr('src').replace('_cut' , "");
        this.photoIndex = Number($photo.attr('index'));
        this.pid = $photo.attr('pid');
        $('.bigPhoto').removeClass('none');
        $('.bigPhoto img').setAttribute('src' , src);
        $('.backstep').addClass('none');
        this.getComments();
        this.getLikeState();
    } ,
    getPhotos : function () {
        var self = this;
        ajax.get({
            url : '/snapshot/image/selectShopPublicImages.do' ,
            data : {
                openId : self.user.openId ,
                shopOpenId : self.shop.openId ,
                no : 1 ,
                size : 10000 ,
                state : 3
            } ,
            success : function (resp) {
                if (resp.success && resp.value && resp.value.shopPublicImages.length) {
                    var photos = resp.value.shopPublicImages;
                    var width = "width:" + ($('.photos').offsetWidth - 15) / 3 + "px";
                    $('.photos').html(template.render('photo-template' , {data : {photos : photos , width : width}}));
                    self.photos = photos;
                } else {
                    $('.photos').addClass('none');
                    $('.noPhotoRemind').removeClass('none');
                    $('.toTakePhoto').href = "takePhotos.html" + window.location.search;
                }
            }
        });
    } ,
    getComments : function () {
        var self = this;
        ajax.get({
            url : '/snapshot/comment/selectCommentsByImageId.do' ,
            data : {
                imageId : self.pid
            } ,
            success : function (resp) {
                $('.comments').removeClass('none').html('')
                if (resp.value.length) {
                    $('.comments').html(template.render("comment-template" , {comments : resp.value}));

                } else {
                    $('.comments').addClass('none');
                }
            }
        });
    } ,
    getLikeState : function () {
        var photo = this.photos[Number(this.photoIndex)];
        if (photo.liked) {
            $('.like img').attr('src' , "../images/redHeart.png").attr('liked' , 'true');
        } else {
            $('.like img').attr('src' , "../images/whiteHeart.png").attr('liked' , 'false');
        }
    } ,
    insertLike : function () {
        var $img = $('.like img') , self = this;
        if ($img.attr('liked') == 'true') {
            ajax.get({
                url : '/snapshot/user/deleteLikeByImageId.do' ,
                data : {
                    imageId : self.pid
                } ,
                success : function (resp) {
                    if (resp.success) {
                        $img.attr('liked' , 'false');
                        $img.src = $img.src.replace('redHeart' , 'whiteHeart');
                        self.photos[self.photoIndex].liked = false;
                    }
                }
            });
        } else {
            ajax.get({
                url : '/snapshot/user/insertLike.do' ,
                data : {
                    imageId : self.pid
                } ,
                success : function (resp) {
                    if (resp.success) {
                        $img.attr('liked' , 'true');
                        $img.src = $img.src.replace('whiteHeart' , 'redHeart');
                        self.photos[self.photoIndex].liked = true;
                    } else {
                        alert(resp.message);
                    }
                } ,
                error : function () {
                    document.title = "insertLike error";
                }
            });
        }
    } ,
    comment : function () {
        var content = $('.comment-box input').value.trim() , self = this;
        if (!content) {
            return alert('评论内容不能为空哦!');
        }
        $('.commitComment').innerText = "提交中...";
        ajax.get({
            url : '/snapshot/user/insertComment.do' ,
            data : {
                imageId : self.pid ,
                content : content
            } ,
            success : function (resp) {
                if (resp.success) {
                    $('.comment-box').addClass('none');
                    $('.comment-box input').value = "";
                    $('.commitComment').innerText = "评论";
                    self.getComments();
                } else {
                    alert(resp.message);
                }
            }
        });
    }
}
window.addEventListener("DOMContentLoaded" , function () {
    main.init();
} , false);